//
//  SYS_AccountSetVC.m
//  System_App
//
//  Created by 伟徐 on 2017/11/28.
//  Copyright © 2017年 czy001. All rights reserved.
//

#import "SYS_AccountSetVC.h"
#import "SYS_MineBaseCell.h"
#import "SYS_MineAccountCell.h"
#import "SYS_AmendPasswordVC.h"
#import "SYS_NicknameVC.h"
#import "ApplyViewController.h"

@interface SYS_AccountSetVC ()<UITableViewDelegate,UITableViewDataSource,SYS_PublicPopViewDelegete>

@property (nonatomic, strong) SYS_PublicPopView *popAlertView;
@property (nonatomic, strong) UITableView*tableView;
@property (nonatomic, copy) NSArray *logoArray;
@property (nonatomic, copy) NSArray *titleArray;

@end

@implementation SYS_AccountSetVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   
    [self initTableView];
    [self setNavigationBar];
}

#pragma mark -- setter and getter
- (void)setNavigationBar{
    self.title = @"账号设置";
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"SYS_Public_back" highImage:nil title:nil target:self action:@selector(accountSetVCBack)];
    self.view.backgroundColor =RGBACOLOR(240, 240, 240, 1);
}

-(void)initTableView{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH,SCREENHEIGHT-NavBarHeight) style:UITableViewStyleGrouped];
    _tableView.delegate =self;
    _tableView.dataSource =self;
    _tableView.backgroundColor =RGBACOLOR(240, 240, 240, 1);
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview: _tableView];
    [_tableView registerClass:[SYS_MineBaseCell class] forCellReuseIdentifier:@"SYS_MineBaseCell"];
    [_tableView registerClass:[SYS_MineAccountCell class] forCellReuseIdentifier:@"SYS_MineAccountCell"];
    
    UIButton *logoutBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    logoutBtn.frame =CGRectMake(20, 65*self.logoArray.count+15, SCREENWIDTH-40, 48);
    logoutBtn.backgroundColor = MainColor;
    logoutBtn.layer.cornerRadius = 24;
    logoutBtn.layer.masksToBounds = YES;
    logoutBtn.titleLabel.font =[UIFont systemFontOfSize:18];
    [logoutBtn setTitle:@"退出登录" forState:UIControlStateNormal];
    [logoutBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [logoutBtn addTarget:self action:@selector(gotoLogout) forControlEvents:UIControlEventTouchUpInside];
    [_tableView addSubview:logoutBtn];
}

#pragma mark -- touch and event
- (void)accountSetVCBack{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - SYS_PublicPopViewDelegete
-(void)btnindex:(int)index{
    if(index == 1001){
        [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
        [[ApplyViewController shareController] clear];
        
        //删除归档文件
        NSFileManager *defaultManager = [NSFileManager defaultManager];
        if ([defaultManager isDeletableFileAtPath:LoginFilePath]) {
            [defaultManager removeItemAtPath:LoginFilePath error:nil];
        }
        
        //删除存储信息
        [SYS_LoginModel instance].sessionInfo=nil;
        [SYS_LoginModel instance].user=nil;
        [SYS_LoginModel instance].admin=nil;
        
        if ([EMClient sharedClient].isLoggedIn) {
            __weak SYS_AccountSetVC *weakSelf = self;
         dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                EMError *error = [[EMClient sharedClient] logout:YES];
                dispatch_async(dispatch_get_main_queue(), ^{
                    [weakSelf hideHud];
                    if (error != nil) {
                        [weakSelf showHint:error.errorDescription];
                    }
                    else{
                        [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@NO userInfo:@{@"isLogin":@"no"}];
                    }
                });
            });
        }
        else
        {
            [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@NO userInfo:@{@"isLogin":@"no"}];
        }
    }
    [_popAlertView dismmis1];
}

- (void)gotoLogout{
    
    _popAlertView =[[SYS_PublicPopView alloc] initwithtitleText:@"退出登录" withMessage:@"您确定要退出登录吗?" andcancelbtn:@"取消" andotherbtn:@"确定" withIconStr:@"SYS_Public_tuichu" andCancelBtnImageNameStr:@"SYS_Public_quxiao" andSureBtnImageNameStr:@"SYS_Public_queding"];
    _popAlertView.delegate =self;
    [_popAlertView show];
}

#pragma mark - UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.logoArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row == 0){
        SYS_MineAccountCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SYS_MineAccountCell" forIndexPath:indexPath];
        cell.selectionStyle =  UITableViewCellSelectionStyleNone;
        cell.logoImg.image= [UIImage imageNamed:self.logoArray[indexPath.row]];
        cell.titleLabel.text= self.titleArray[indexPath.row];
        cell.contentLabel.text=[SYS_LoginModel instance].user.phone;
        return cell;
        
    }else{
        SYS_MineBaseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SYS_MineBaseCell" forIndexPath:indexPath];
        cell.selectionStyle =  UITableViewCellSelectionStyleNone;
        cell.logoImg.image= [UIImage imageNamed:self.logoArray[indexPath.row]];
        cell.titleLabel.text= self.titleArray[indexPath.row];
        cell.rightImg.image= [UIImage imageNamed:@"Mine_Next"];
        if(indexPath.row == 1){
            cell.contentLabel.text=[SYS_LoginModel instance].user.nickName;
        }
        return cell;
    }
   
}


#pragma mark - UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 65;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01f;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.01f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 0:
            break;
        case 1:{
            SYS_NicknameVC *vc = [[SYS_NicknameVC alloc] init];
            vc.nickname =[SYS_LoginModel instance].user.nickName;
            vc.saveClickBlock = ^{
                Dlog(@"+++++%@",[SYS_LoginModel instance].user.nickName);
                [_tableView reloadData];
            };
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 2:{
            SYS_AmendPasswordVC *vc = [[SYS_AmendPasswordVC alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
            
        default:
            break;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}

-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}

#pragma mark - 懒加载控件
-(NSArray *)logoArray{
    if(!_logoArray){
        _logoArray = [NSArray arrayWithObjects:@"Mine_Account",@"Mine_Nickname",@"Mine_AmendPSW",nil];
    }
    return _logoArray;
}

-(NSArray *)titleArray{
    if(!_titleArray){
        _titleArray = [NSArray arrayWithObjects:@"登录账号",@"昵称",@"修改密码", nil];
    }
    return _titleArray;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
